﻿'    DotNetIRCD - An IRC Daemon written in VB.Net
'    Copyright (C) 2012 GeorgeBush
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program. If not, see <http://www.gnu.org/licenses/>.

''' <summary>
''' This is the logging provider for this Application
''' </summary>
Public Class LoggingProvider

    ''' <summary>
    ''' 
    ''' </summary>
    Public Property LogSettings As New List(Of LogSetting)

    ''' <summary>
    ''' The FileName to use when logging to file
    ''' </summary>
    Public Property LogFileName As String

    ''' <summary>
    ''' Log a message using any relevant Provider
    ''' </summary>
    ''' <param name="MessageType">The type of message</param>
    ''' <param name="Message">The Message to log</param>
    Public Sub Log(ByVal MessageType As LogTypeEnum, ByVal Message As String)

        ' Cycle through all of the targets
        For Each ThisLogSetting As LogSetting In LogSettings

            ' Decide what to do based on the type of log message we have been passed
            If MessageType = LogTypeEnum.Info Then

                ' If this target is not an ErrorOnly target and it is set to log Info then log Info
                If Not ThisLogSetting.LogLevel = LogLevelEnum.ErrorOnly Then
                    If ThisLogSetting.LogLevel >= LogLevelEnum.Info Then
                        Logging.Log(MessageType, ThisLogSetting.LogTarget, Message)
                    End If
                End If

            ElseIf MessageType = LogTypeEnum.Fatal Or MessageType = LogTypeEnum.Error Then

                ' Always log Errors and Fatal Errors to every LogTarget
                Logging.Log(MessageType, ThisLogSetting.LogTarget, Message)

            ElseIf MessageType = LogTypeEnum.Warn Or MessageType = LogTypeEnum.InfoExt Then

                ' If this target is not an ErrorOnly target and it is set to log anything 
                ' verbose then log this message
                If Not ThisLogSetting.LogLevel = LogLevelEnum.ErrorOnly Then
                    If ThisLogSetting.LogLevel >= LogLevelEnum.Verbose Then
                        Logging.Log(MessageType, ThisLogSetting.LogTarget, Message)
                    End If
                End If

            ElseIf MessageType = LogTypeEnum.Debug Then

                ' If this is not an ErrorOnly target and it accepts debug messages then log this message
                If Not ThisLogSetting.LogLevel = LogLevelEnum.ErrorOnly Then
                    If ThisLogSetting.LogLevel = LogLevelEnum.Debug Then
                        Logging.Log(MessageType, ThisLogSetting.LogTarget, Message)
                    End If
                End If

            End If

        Next

    End Sub

End Class

''' <summary>
''' This structure represents a logging method/target
''' </summary>
Public Structure LogSetting

    ''' <summary>
    ''' The LogTarget this logging method uses
    ''' </summary>
    Public Property LogTarget As LogTargetEnum

    ''' <summary>
    ''' The level of logging to perform
    ''' </summary>
    Public Property LogLevel As LogLevelEnum

    ''' <summary>
    ''' Creates a new instance of a structure that represents a logging method/target
    ''' </summary>
    ''' <param name="LogTarget">The LogTarget this logging method uses</param>
    ''' <param name="LogLevel">The level of logging to perform</param>
    Sub New(ByVal LogTarget As LogTargetEnum, ByVal LogLevel As LogLevelEnum)

        Me.LogTarget = LogTarget
        Me.LogLevel = LogLevel

    End Sub

End Structure

''' <summary>
''' Enumeration of Logging levels
''' </summary>
Public Enum LogLevelEnum As Integer

    ''' <summary>
    ''' Only log Error's and Information Messages
    ''' </summary>
    Info = 1

    ''' <summary>
    ''' Log Info, Error, InfoEx and Warnings
    ''' </summary>
    Verbose = 2

    ''' <summary>
    ''' Log all Messages
    ''' </summary>
    Debug = 3

    ''' <summary>
    ''' Only log Errors
    ''' </summary>
    ErrorOnly = 4

End Enum